scriptSupport.addInit(function() {
	var doc = Doc(document);
	doc.contentPanel = new ContentPanel(doc);
	StatusDisplay.getInstance();
});

function ContentPanel(doc) {
	this.doc = doc;
	var el = Dom(this.doc.doc.body).getFirstChildByClassName("contentPanel");
	if (!el) {
		el = this.doc.doc.createElement("div");
		while (this.doc.doc.body.childNodes.length > 0)
			el.appendChild(this.doc.doc.body.childNodes[0]);
		el.className = "contentPanel";
		this.doc.doc.body.appendChild(el);
		el.id = this.doc.doc.body.id;
	}
	ContentPanel._super.call(this, el);
	this.cleanup();
}
Object.extend("ContentPanel", "E.Dom");

ContentPanel.prototype.merge = function(el, restoreSelection) {
	if (!el || !el.hasChildNodes())
		return;
	var d = Dom(el);
	var sel = this.doc.selectionSupport;
	var selId;
	if (restoreSelection && sel && sel.current)
		selId = sel.current.entityView.dom.element.id;
	if (typeof (EntityView) == "function")
		EntityView.entityViews = null;
	d.cleanup();
	d.mergeInto(this.element);
	this.cleanup();
	this.element.className = "contentPanel";
	var dyns = this.element.getElementsByTagName("iframe");
	for ( var i = 0; i < dyns.length; ++i)
		dyns[i].contentWindow.location.reload();
	this.doc.applyEditFeatures && this.doc.applyEditFeatures();
	if (selId)
		sel.select(Dom(selId).getEntityView());
}

ContentPanel.prototype.getViewUri = function() {
	return this.doc.getViewUri();
}

function StatusDisplay(doc) {
	this.doc = doc;
	this.dom = this.doc.createElement("div");
	this.dom.className = "status_display";
	this.doc.body.appendChild(this.dom);
}

StatusDisplay.getInstance = function() {
	if (!document.statusDisplay)
		document.statusDisplay = new StatusDisplay(document);
	return document.statusDisplay;
}

StatusDisplay.prototype.createLog = function(timeout) {
	return new StatusLog(this, timeout);
}

function StatusLog(display, timeout) {
	this.dom = display.doc.createElement("div");
	this.fadeSupport = new FadeSupport(this.dom);
	this.fadeSupport.hide();
	display.dom.appendChild(this.dom);
	this.msgs = new Array();
	this.timeout = (timeout)?timeout:20000;
}

StatusLog.prototype.show = function(text, url, replaceLast, timeout) {
	if (null == this.dom)
		return false;
	this._show(text, "status_msg", url, replaceLast, timeout);
}

StatusLog.prototype.showError = function(text, url) {
	if (null == this.dom)
		return false;
	this.stick = true;
	for ( var i = 0; i < this.msgs.length; i++)
		this.msgs[i].show();
	this._show(text, "status_error", url);
}

StatusLog.prototype._show = function(text, className, url, replaceLast, timeout) {
	this._clearTimer(true);
//	console.log(text +" "+ this.msgs.length);
	var msg = new StatusMessage(this, text, className, url);
	if (this.msgs.length > 0) {
		var lastMsg = this.msgs[this.msgs.length - 1];
		if (replaceLast || lastMsg.equals(msg)) {
			this.dom.removeChild(lastMsg.dom);
			this.msgs[this.msgs.length - 1] = msg;
		}
		else {
			this.msgs[this.msgs.length] = msg;
		}
	}
	else {
		this.msgs[this.msgs.length] = msg;
	}
	this.fadeSupport.show();
	if (!this.stick) {
		this.timerId = window.setTimeout(function(display) {
			display.hide();
		}, (timeout)?timeout:3000, this);
		this.cleanupTimerId = window.setTimeout(function(display) {
			display.cleanup();
		}, this.timeout, this);
	}
}

StatusLog.prototype._clearTimer = function(cleanup) {
	if (this.timerId) {
		var id = this.timerId;
		this.timerId = null;
		window.clearTimeout(id);
	}
	if (cleanup && this.cleanupTimerId) {
		var id = this.cleanupTimerId;
		this.cleanupTimerId = null;
		window.clearTimeout(id);
	}
}

StatusLog.prototype.hide = function() {
	this._clearTimer();
	this.fadeSupport.fadeOut(true, false);
}

StatusLog.prototype.cleanup = function() {
	this._clearTimer(true);
	this.fadeSupport.fadeOut(true, true);
	this.dom = null;
}

function StatusMessage(log, text, className, url) {
	this.dom = log.dom.ownerDocument.createElement("div");
	log.dom.appendChild(this.dom);
	if (url)
		this.dom.innerHTML = "<a href=\"" + BasePath.get() + url + "\">" + text
		    + "</a>";
	else
		this.dom.innerHTML = text;
	this.dom.className = className;
}

StatusMessage.prototype.equals = function(value) {
	return this.dom.innerHTML == value.dom.innerHTML;
}

StatusMessage.prototype.show = function() {
	Dom(this.dom).show();
}

StatusMessage.prototype.hide = function() {
	Dom(this.dom).hide();
}
